<?
//Paragraph 0.0.5
class paragraph{
    private $id='';
    private $lng_id='';
    private $title='';
    private $content='';
    public function __construct($id='', $lng_id='', $title='', $content=''){
        global $sql, $kernel;
        $this->id=$id;
        $this->lng_id=$lng_id?$lng_id:$kernel->lang->get('id');
        $this->title=$title;
        $this->content=$content;
    }
    //Read data from db
    public function read($lng_id=''){
        global $sql;
        if($lng_id) $this->lng_id=$lng_id;
        $r=$sql->select_each("paragraphs", array('title', 'content'), array('id'=>$this->id, 'lng_id'=>$this->lng_id), "", "", 1);
        if(is_array($r)){
            $this->title=$r['title'];
            $this->content=$r['content'];
        }
        else{
            $this->title='';
            $this->content='';
        }
        return array($this->title, $this->content);
    }
    //Edit the paragraph
    public function edit($lng_id='', $title='', $content=''){
        global $sql;
        if($lng_id) $this->lng_id=$lng_id;
        if($title) $this->title=$title;
        if($content) $this->content=$content;
        $r=$sql->update("paragraphs", array('title'=>$this->title, 'content'=>$this->content), array('id'=>$this->id, 'lng_id'=>$this->lng_id), 1);
        return $r;
    }
    //Add a paragraph
    public function add($lng_id='', $title='', $content=''){
        global $sql;
        if($lng_id) $this->lng_id=$lng_id;
        if($title) $this->title=$title;
        if($content) $this->content=$content;
        $values=array('title'=>$this->title, 'content'=>$this->content, 'lng_id'=>$this->lng_id);
        if($this->id) $values['id']=$this->id;
        $r=$sql->insert("paragraphs", $values);
        $this->id=mysql_insert_id();
        return $r?$this->id:'';
    }
    public function set($lng_id='', $title='', $content=''){
        global $sql;
        if($lng_id) $this->lng_id=$lng_id;
        $this->title=$title;
        $this->content=$content;
        if($sql->count('paragraphs', array('id'=>$this->id, 'lng_id'=>$this->lng_id))){
            $this->edit();
        }
        else{
            $this->add();
        }
    }
    public function del($lng_id='-'){
        global $sql;
        if($lng_id) $this->lng_id=$lng_id;
        $values=array('id'=>$this->id);
        if($this->lng_id&&$this->lng_id!='-') $values['lng_id']=$lng_id;
        $sql->delete('paragraphs', $values);
    }
    public function __get($key){
        if(isset($this->$key)) return $this->$key;
        else return '';
    }
    public function __set($key, $val){
        $this->$key=$val;
    }
}
?>